<?php

namespace app\web\controller;
use app\web\model\CashmanageModel;
use app\web\model\CashModel;
use app\web\model\AgentModel;
use app\common\controller\StoreBaseController;
use think\Db;
use app\common\model\Csv;
/**
 * 提现列表
 */
class CashmanageController extends StoreBaseController
{
 	protected $model;
	protected $cash_model;
	protected $agent_model;
	protected $csv;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new CashmanageModel();
		$this->cash_model = new CashModel();
		$this->agent_model = new AgentModel();
		$this->csv = new Csv();
		$cash_list = $this->cash_model->where('uid',session('storeInfo')['u_id'])->select();	
		$this->assign('cash_list',$cash_list);
		//获取商家余额
		$agentinfo  = $this->agent_model->where('id',session('storeInfo')['id'])->find();
		$this->assign('store_balance',$agentinfo['store_balance']);

    }
    /**
     * 提现列表
     * @author [longx] 
     */
    public function index( $page = 1){
 		$field = 'm.*,g.id,g.mobile,g.real_name';
		$map['uid'] = session('storeInfo')['u_id'];  	
		if(request()->isPost()){
		 	$param = input('post.');
			//用户真实姓名
			if($param['real_name']){
				$map['real_name'] = ['like', '%'.$param['real_name'].'%'];
			}
			//提现类型
			if($param['type'] != 0){
				$map['type'] = $param['type'];
			}
			//提现状态
			if($param['cash_status'] != 0){
				$map['cash_status'] = $param['cash_status'];
			}
			//提现账户
			if($param['cash_type'] != 0){
				$map['cash_type'] = $param['cash_type'];
			}   				
		 }
		$list = Db::name("mall_cashmanage")->alias('m')->join('__MEMBER__ g','g.id=m.uid')->field($field)->where($map)->order("m.createtime desc")->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 
     * 提现申请
     * @author [longx] 
     */
    public function withdrawals()
    {
    	$agentinfo = session('storeInfo');
        if(request()->isPost()){       	
        	$param = input('post.');
			$param['uid'] = $agentinfo['u_id'];			
			$param['user_type'] = $agentinfo['agent_type']; //用户类型
			$param['createtime'] = now_time();
			$param['actual_balance'] = 	$param['balance'] - $param['counter_fee']; //实际提现金额
			$paytype = $param['cash_type'];
			$massage = '';
			if($paytype == 1 && $param['card_no']){
				$massage = $param['bank_account'].' '.$param['bank_username'].' '.$param['card_no'];
			}elseif($paytype == 2 && $param['alipay']){
				$massage = $param['alipay_nickname'].' '.$param['alipay'];
			}elseif($paytype == 3 && $param['wechat_account']){
				$massage = $param['wechat_account'].' '.$param['wechat_account'];
			}else{
				$this->error('请绑定提现方式！');
			}
			$param['message'] = $massage;
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Cashmanage/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Cashmanage/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
      //导出提现列表
    
    
    public function dc(){
    	$field = 'm.*,g.id,g.mobile,g.real_name';
		$map['uid'] = session('storeInfo')['u_id'];  
    	$list = Db::name("mall_cashmanage")->alias('m')
    	->join('__MEMBER__ g','g.id=m.uid')
    	->field($field)->where($map)
    	->order("m.createtime desc")->select();
	$result=array();
  foreach($list as $k=>$v){
  	$result[$k]['name']=$v['real_name'];
  	$result[$k]['mobile']=$v['mobile'];
  	$result[$k]['balance']=$v['balance'];
  			if ($v['user_type'] == 1){
                        		$result[$k]['user_type']='普通用户';
                      }elseif($v['user_type'] == 2){
                      	$result[$k]['user_type']='商户';
                      }else{
                        			$result[$k]['user_type']='总代理';	
    		}
    		if ($v['cash_type'] == 1){
                        		$result[$k]['cash_type']='银行卡'.$v['message'];
                      }elseif($v['cash_type'] == 2){
                      	$result[$k]['cash_type']='支付宝'.$v['message'];
                      }else{
                        $result[$k]['cash_type']='微信支付'.$v['message'];	
    		}
    		if ($v['type'] == 1){
                        		$result[$k]['type']='T+0';
                      }elseif($v['type'] == 2){
                      	$result[$k]['type']='T+1';
                      }else{
                        			$result[$k]['type']='T+2';	
    		}
    		$result[$k]['actual_balance']=$v['actual_balance'];
    		$result[$k]['counter_fee']=$v['counter_fee'];
    		if ($v['cash_status'] == 1){
                        		$result[$k]['cash_status']='处理中';
                      }elseif($v['cash_status'] == 2){
                      	$result[$k]['cash_status']='提现成功';
                      }else{
                        			$result[$k]['cash_status']='提现失败';	
    		}
    		$result[$k]['createtime']=$v['createtime'];
  }	

   			$str="提现人,电话,提现金额,用户类型,提现账户,提现类型,实际到账金额,手续费,状态,申请时间,";//设置导出第一行
        	$list="name,mobile,balance,user_type,cash_type,type,actual_balance,counter_fee,cash_status,createtime";//设置导出字段
        	$filename ='提现列表.csv';//设置文件名
        	$this->csv->dc($str,$list,$result,$filename);
    }
}
